package com.zzyl.controller.security;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeptDto;
import com.zzyl.service.DeptService;
import com.zzyl.vo.DeptVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门前端控制器
 */
@Slf4j
@Api(tags = "部门管理")
@RestController
@RequestMapping("/dept")
@RequiredArgsConstructor
public class DeptController {
	
	private final DeptService deptService;
	
	
	/**
	 * 保存部门
	 * @param deptDto 部门DTO对象
	 * @return Boolean
	 */
	@PutMapping
	@ApiOperation(value = "部门添加", notes = "部门添加")
	@ApiImplicitParam(name = "deptDto", value = "部门DTO对象", required = true, dataType = "DeptDto")
	@ApiOperationSupport(includeParameters = {
			"deptDto.dataState",
			"deptDto.deptName",
			"deptDto.leaderId",
			"deptDto.remark",
			"deptDto.sortNo",
			"deptDto.parentDeptNo"})
	public ResponseResult<DeptVo> createDept(@RequestBody DeptDto deptDto) {
		deptService.createDept(deptDto);
		return ResponseResult.success();
	}
	
	/**
	 * 修改部门
	 * @param deptDto 部门DTO对象
	 * @return Boolean 是否修改成功
	 */
	@PatchMapping
	@ApiOperation(value = "部门修改", notes = "部门修改")
	@ApiImplicitParam(name = "deptDto", value = "部门DTO对象", required = true, dataType = "DeptDto")
	public ResponseResult<Boolean> updateDept(@RequestBody DeptDto deptDto) {
		deptService.updateDept(deptDto);
		return ResponseResult.success();
	}
	
	@PatchMapping("/is_enable")
	@ApiOperation(value = "启用-禁用", notes = "启用-禁用")
	@ApiImplicitParam(name = "deptDto", value = "部门DTO对象", required = true, dataType = "DeptDto")
	@ApiOperationSupport(includeParameters = {"deptDto.dataState", "deptDto.id"})
	public ResponseResult<Boolean> isEnable(@RequestBody DeptDto deptDto) {
		deptService.isEnable(deptDto);
		return ResponseResult.success();
	}
	
	/**
	 * 删除部门
	 */
	@ApiOperation("删除部门")
	@DeleteMapping("/{deptId}")
	public ResponseResult remove(@PathVariable String deptId) {
		deptService.deleteDeptById(deptId);
		return ResponseResult.success();
	}
	
	/***
	 *  多条件查询部门列表
	 * @param deptDto 部门DTO对象
	 * @return List<DeptVo>
	 */
	@PostMapping("/list")
	@ApiOperation(value = "部门列表", notes = "部门列表")
	@ApiImplicitParam(name = "deptDto", value = "部门DTO对象", required = true, dataType = "DeptDto")
	@ApiOperationSupport(includeParameters = {"deptDto.dataState", "deptDto.deptName", "deptDto.parentDeptNo"})
	public ResponseResult<List<DeptVo>> deptList(@RequestBody DeptDto deptDto) {
		List<DeptVo> deptVoList = deptService.findDeptList(deptDto);
		return ResponseResult.success(deptVoList);
	}
	
	/**
	 * 组织部门树形
	 * @return
	 */
	@PostMapping("/tree")
	@ApiOperation(value = "部门树形", notes = "部门树形")
	public ResponseResult<TreeVo> deptTreeVo(@RequestBody DeptDto deptDto) {
		TreeVo treeVo = deptService.deptTreeVo(deptDto);
		return ResponseResult.success(treeVo);
	}
}
